package org.example.proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;

public class MyFactoryBean<T> extends AbstractFactoryBean<T> {
    private final Class<T> interF;

    public MyFactoryBean(Class<T> interF) {
        this.interF = interF;
    }

    @Override
    public Class<?> getObjectType() {
        return interF;
    }

    @Override
    protected T createInstance() throws Exception {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addInterface(interF);
        proxyFactory.addAdvice(new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("before");
                Object result = invocation.proceed();
                System.out.println("after");
                return result;
            }
        });
        return (T) proxyFactory.getProxy();
    }
}
